﻿@inherits LayoutComponentBase
@implements IDisposable
@inject IJSRuntime Js
@inject ISysMenuService SysMenuService
@inject ISysAuthService SysAuthService

<CascadingValue Value="@State">
    <ErrorLogger>
        @if (Context!.IsAuthenticated)
        {
            <StackLayout Sider>
                <StackSider>
                    <StackSiderContent>
                        <BaseMenu/>
                    </StackSiderContent>
                </StackSider>

                <StackLayout>
                    <StackHeader Fixed>
                        <BaseTopbar/>
                    </StackHeader>

                    <StackContent class="m-3">
                        @if (State.UseTabSet)
                        {
                            <RenderTemplate>
                                <Tab ClickTabToNavigation="true" 
                                    ShowExtendButtons="true" 
                                    ShowClose="true"
                                    DefaultUrl="/"
                                    AdditionalAssemblies="new[] { GetType().Assembly }"
                                    Body="@(Body!)"/>
                            </RenderTemplate>
                        }
                        else
                        {           
                            @Body
                        }

                    </StackContent>

                    @if (State.ShowFooter)
                    {
                        <Footer Text="Admin.NET.Blazor"/>
                    }

                </StackLayout>
            </StackLayout>

            <BaseSetting/>
        }
        else
        {
            <BaseLogin/>
        }
    </ErrorLogger>
</CascadingValue>

@code
{
    /// <summary>
    /// 当前Token信息
    /// </summary>
    [CascadingParameter]
    private TokenContext? Context { get; set; }

    /// <summary>
    /// 当前会话状态
    /// </summary>
    private SessionState State { get; set; } = new();

    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        State.FragmentChanged += OnFragmentChanged;
        await LoadUserDataAsync();
    }

    /// <summary>
    /// OnAfterRenderAsync
    /// </summary>
    /// <param name="firstRender"></param>
    /// <returns></returns>
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await Js.InvokeVoidAsync("$.loaded");
        }
    }

    /// <summary>
    /// 重载整个布局
    /// </summary>
    /// <returns></returns>
    public async void OnFragmentChanged(object? sender, bool args)
    {
        if (args)
        {
            await LoadUserDataAsync();
        }
        await InvokeAsync(StateHasChanged);
    }

    /// <summary>
    /// 获取用户信息（包含权限、角色等）
    /// </summary>
    /// <returns></returns>
    private async Task LoadUserDataAsync()
    {
        //重新获取菜单数据
        if (Context!.IsAuthenticated)
        {
            State.Auths = await SysAuthService.GetLoginUserAsync();
            State.Menus = await SysMenuService.GetCurrentUserMenusAsync(State.Auths?.Apps);
            State.Leaves = State.Menus.SelectMany(x => x.GetAllSubItems().Where(y => !y.Items.Any())).ToList();
        }
    }

    /// <summary>
    /// Dispose
    /// </summary>
    public void Dispose() => State.FragmentChanged -= OnFragmentChanged;
}
